/*
 * CUDAAESDecrypter.cpp
 *
 *  Created on: Jun 23, 2010
 *      Author: lukasz
 */

#include "CUDAAESDecrypter.h"
#include "Encrypter.h"
#include "cudaUtils.h"

CUDAAESDecrypter::CUDAAESDecrypter(BigInteger& init_vector, byte* key)
	:AESDecrypter(init_vector, key)
	,d_cuMem(new CudaMemoryManager())
	,device_IVMemory(NULL)
	,deviceIVmemorySize(0)
{
	assert(sizeof(d_key.rd_key) == AESEncrypter::s_expandedKeySize);
	deviceMemExpandedKey =(uint32_t*) d_cuMem->malloc(AESEncrypter::s_expandedKeySize/sizeof(uint32_t));
	d_cuMem->copy((byte*)deviceMemExpandedKey, (byte*)d_key.rd_key, AESEncrypter::s_expandedKeySize);
}

CUDAAESDecrypter::~CUDAAESDecrypter()
{
	// TODO Auto-generated destructor stub
}

void CUDAAESDecrypter::decrypt(void* buffer, uint32_t size)
{
/*	BigInteger ivc = iv;
	ivc = ivc + count;

	aesEncryptIVBlocks((byte*)buffer,
						  host_IVMemory,
						  device_IVMemory,
						  size,
						  deviceMemExpandedKey,
						  ivc);
*/
}

void CUDAAESDecrypter::allocDeviceIVMemory(uint32_t size)
{
	if (size > deviceIVmemorySize)
	{
		if (device_IVMemory)
		{
			d_cuMem->free(device_IVMemory);
			delete[] host_IVMemory;
		}
		device_IVMemory = d_cuMem->malloc(size);
		host_IVMemory = new byte[size];
	}
}
